package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewAssignmentDetail;
import vn.student.ViewEditSemesterDetailEditor;
import vn.student.ViewSemesterDetail;
import vn.student.mylib.Assignment;
import vn.student.mylib.Constants;
import vn.student.mylib.ListAssignmentInPlannerAdapter;
import vn.student.mylib.ListSemesterAdapter;
import vn.student.mylib.Semester;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class ViewPlannerController {
	Activity act = null;
	AStudentDB myDB = null;
	int isDone = 0;
	public ViewPlannerController(Activity act){
		this.act = act;
		myDB = new AStudentDB(this.act);
		RelativeLayout viewInstructorContact = (RelativeLayout) act.findViewById(R.id.viewAssignmentsBtn);
		viewInstructorContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewAssignments();
			}
		});
		RelativeLayout viewStudentContact = (RelativeLayout) act.findViewById(R.id.viewSemestersBtn);
		viewStudentContact.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				viewSemesters();
			}
		});
		this.viewSemesters();
	}
	public void viewSemesters(){
		LayoutInflater inflater=act.getLayoutInflater();
		((RelativeLayout)act.findViewById(R.id.bodyPlanner)).removeAllViews();
		inflater.inflate(R.layout.semester_list, (RelativeLayout)act.findViewById(R.id.bodyPlanner));
		RelativeLayout tmp = (RelativeLayout) act.findViewById(R.id.addNewSemesterBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickAddNewSemesterButton();
			}
		});
		myDB = new AStudentDB(act);
		ArrayList<Semester> listSemester = myDB.getSemesterList();
		if(listSemester!=null){
			ListView listSemesterView = (ListView)act.findViewById(R.id.listSemesterView);
			listSemesterView.setAdapter(new ListSemesterAdapter(act,listSemester));
			listSemesterView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
						long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(act,ViewSemesterDetail.class);
					Bundle bundle = new Bundle();
					ListSemesterAdapter adapter = (ListSemesterAdapter) arg0.getAdapter();
					ArrayList<Semester> list = adapter.list;
					bundle.putString("id", list.get(arg2).id);
					intent.putExtras(bundle);
					act.startActivityForResult(intent, Constants.ViewSemesterDetailRequestCode);
				}
			});
			listSemesterView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()  {
				@Override
				public void onCreateContextMenu(ContextMenu menu, View v,
						ContextMenuInfo menuInfo) {
					menu.add(0,Constants.PLANNER_CONTEXTMENU_ADD_SEMESTER,0, "Add new");
					menu.add(0,Constants.PLANNER_DETAIL_CONTEXTMENU_EDIT_SEMESTER,1, "Edit");
					menu.add(0,Constants.PLANNER_DETAIL_CONTEXTMENU_DELETE_SEMESTER,2, "Delete");
				}
	       }); 
		}
	}
	public void viewAssignments(){
		LayoutInflater inflater=act.getLayoutInflater();
		((RelativeLayout)act.findViewById(R.id.bodyPlanner)).removeAllViews();
		inflater.inflate(R.layout.planner_ass, (RelativeLayout)act.findViewById(R.id.bodyPlanner));
		RelativeLayout tmp = (RelativeLayout) act.findViewById(R.id.viewUndoneAssBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isDone = 0;
				showAssignments();
			}
		});
		tmp = (RelativeLayout) act.findViewById(R.id.viewDoneAssBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isDone = 1;
				showAssignments();
			}
		});
		tmp = (RelativeLayout) act.findViewById(R.id.viewAllAssBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				isDone = 2;
				showAssignments();
			}
		});
		this.showAssignments();
	}
	public void showAssignments(){
		ArrayList<Assignment> listAss = myDB.getAssignmentList(isDone);
		if(listAss==null) 
			return;
		ListView listAssignmentView = (ListView) act.findViewById(R.id.listView);
		listAssignmentView.setAdapter(new ListAssignmentInPlannerAdapter(act,listAss,true));
		listAssignmentView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(act,ViewAssignmentDetail.class);
				Bundle bundle = new Bundle();
				ListAssignmentInPlannerAdapter adapter = (ListAssignmentInPlannerAdapter) arg0.getAdapter();
				ArrayList<Assignment> list = adapter.list;
				bundle.putString("id", list.get(arg2).id);
				bundle.putBoolean("isEdit", false);
				bundle.putBoolean("isCreateNew", false);
				bundle.putString("classId", "-1");
				intent.putExtras(bundle);
				act.startActivityForResult(intent, Constants.ViewAssignmentDetailInPlannerRequestCode);
			}
		});
	}
	public void onClickAddNewSemesterButton(){
		Intent intent = new Intent(act,ViewEditSemesterDetailEditor.class);
		Bundle bundle = new Bundle();
		bundle.putString("id", "-1");
		bundle.putBoolean("isCreateNew", true);
		intent.putExtras(bundle);
		act.startActivityForResult(intent, Constants.ViewAddNewSemesterDetailEditorRequestCode);
	}
	public void removeSemester(int position) {
		// TODO Auto-generated method stub 
		ListView listSemesterView = (ListView)act.findViewById(R.id.listSemesterView);
		ArrayList<Semester> listSemester = ((ListSemesterAdapter)listSemesterView.getAdapter()).list;
		if(myDB.removeSemester(listSemester.get(position).id)){
			listSemester.remove(position);
			if(listSemester!=null&&listSemester.size()==0){
				 ((RelativeLayout) act.findViewById(R.id.addNewSemesterBtn)).setBackgroundResource(R.drawable.class_row_all);
			}
			listSemesterView.setAdapter(new ListSemesterAdapter(act,listSemester));	
		}
		
	}
	public void editSemester(int position) {
		// TODO Auto-generated method stub
		ListView listSemesterView = (ListView)act.findViewById(R.id.listSemesterView);
		ArrayList<Semester> listSemester = ((ListSemesterAdapter)listSemesterView.getAdapter()).list;
		Intent intent = new Intent(act,ViewEditSemesterDetailEditor.class);
		Bundle bundle = new Bundle();
		bundle.putString("id", listSemester.get(position).id);
		bundle.putBoolean("isCreateNew", false);
		intent.putExtras(bundle);
		act.startActivityForResult(intent, Constants.ViewSemesterDetailEditorRequestCode);
	}
}
